package com.rzl.expense.web.controller.system;

import com.rzl.expense.common.constant.Constants;
import com.rzl.expense.common.core.domain.AjaxResult;
import com.rzl.expense.common.core.domain.entity.SysMenu;
import com.rzl.expense.common.core.domain.entity.SysUser;
import com.rzl.expense.common.core.domain.model.LoginBody;
import com.rzl.expense.common.utils.SecurityUtils;
import com.rzl.expense.common.utils.StringUtils;
import com.rzl.expense.common.utils.rsa.ParamParse;
import com.rzl.expense.common.utils.sign.SooAesUtils;
import com.rzl.expense.framework.web.service.SysLoginService;
import com.rzl.expense.framework.web.service.SysPermissionService;
import com.rzl.expense.system.service.ISysMenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author expense
 */
@RestController
@Slf4j
public class SysLoginController {
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isEmpty(loginBody.getPassword())) {
            return AjaxResult.error("密码不能为空");
        }
        String pass = ParamParse.parseStr(loginBody.getPassword());
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), pass, loginBody.getCode(),
                loginBody.getUuid());

        ajax.put(Constants.TOKEN, token);
        return ajax;
    }


    /**
     * sso登录
     * http://36.155.105.97:8861/prod-api/sso/login?token=zyfr/fwKWSFyB+jTTbBmRRLbPbDyj8awkI0Mbgkj4JA=
     *
     * @param token
     * @param
     * @return {@link AjaxResult}
     */
    @GetMapping("/sso/login")
    public AjaxResult ssoLogin(String token) {
        log.info("sso登录,token:{}", token);
        // 判断用户加密的数据不为空
        if (StringUtils.isEmpty(token)) {
            return AjaxResult.error("SMAP账号不能为空");
        }
        try {
            // 获取解密后的数据
            String decrypt = SooAesUtils.Decrypt(token);
            log.info("sso登录,token解密字符串为:{}", decrypt);
            // NCMS|xingzhe@hq.cmcc
            String smapid = decrypt.split("\\|")[1];
            log.info("sso登录,smapid:{}", smapid);
            // 生成令牌
            return AjaxResult.success().put(Constants.TOKEN, loginService.sooLogin(smapid));
        } catch (Exception e) {
            log.error("sso登录,异常:{}", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters() {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
